﻿using System;
using System.Data;
using System.Data.Common;
using System.Configuration;
using yq.CacheHelp;

namespace yq.IBase.DBHelper
{
    /// <summary>
    /// 事物处理
    /// </summary>
    public class DBTrans : IDisposable
    {

        private DbConnection conn;

        private DbTransaction dbTrans;

        /// <summary>
        /// 链接
        /// </summary>
        public DbConnection DbConnection
        {
            get { return this.conn; }
        }

        /// <summary>
        /// 事物
        /// </summary>
        public DbTransaction DbTrans
        {
            get { return this.dbTrans; }
        }

        public DBTrans()
        {
            conn = DbHelper.CreateConnection(DbHelper.DbHelperConnectionWrite);
            conn.Open();
            dbTrans = conn.BeginTransaction();
        }

        public DBTrans(string connectionString): this(connectionString, DbHelper.dbProviderName)
        {
           
        }

        public DBTrans(string connectionString, string dbProviderName)
        {
            conn = DbHelper.CreateConnection(connectionString, dbProviderName);
            conn.Open();
            dbTrans = conn.BeginTransaction();
        }

        public void Dispose()
        {
            this.Close();
        }

        public void Close()
        {
            if (conn.State == System.Data.ConnectionState.Open)
            {
                conn.Close();
            }
            conn.Dispose();
        }
    }
}
